{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import load_tools\n",
    "from langchain.agents import initialize_agent\n",
    "from langchain.agents import AgentType\n",
    "from langchain_openai import OpenAI\n",
    "from langchain.callbacks import StdOutCallbackHandler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(\n",
    "    openai_api_key=\"sk-uRUknVzF2daRmidJ227fEa256c5d4f4b9f9a7bB6CeCd0176\", \n",
    "    base_url=\"https://api.openai-hub.com/v1\",\n",
    "    temperature=0\n",
    "    )\n",
    "\n",
    "handler = StdOutCallbackHandler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/mac/Library/Python/3.9/lib/python/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.\n",
      "  warn_deprecated(\n",
      "/Users/mac/Library/Python/3.9/lib/python/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 0.3.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.\n",
      "Calculator: Useful for when you need to answer questions about math.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search, Calculator]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 高启强的扮演者十年之后多少岁?\n",
      "Thought:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I should use a search engine to find information about the actor 高启强.\n",
      "Action: Search\n",
      "Action Input: 高启强\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m['高启强（1970年～2021年）是电视剧《狂飙》中的男主角。前期为普通市民，中后期为最大反派。该角色由内地男演员张颂文饰演。高启强原本仅是一个底层的卖鱼小贩， ...', '徐纪周向中国新闻周刊表示，高启强原型并非刘汉，原型人物有多个。他说，剧中原型都是扫黑除恶专项斗争开展以来的案例。', '其实，“高启强”有多个现实人物原型，他们有的靠黄赌毒积累原始资产，有的拉拢腐蚀官员，有的肆意欺压百姓……回顾黑恶势力犯罪频发的那个时期，要坚决打击的 ...', '高启强，电视剧《狂飙》中的角色，强盛集团董事长，京海市政协委员，工商联委员，慈善家，由张颂文饰演。高启强致力于社会公益事业，衣着朴素，文质彬彬，是安欣的 ...', '中文名. 高启兰 · 外文名. Gao Qilan · 别名. 小兰（高启强、安欣称呼高启兰） 、兰姐（高启强的手下称呼高启兰） · 饰演. 隆妮 · 性别. 女 · 登场作品. 狂飙 · 职业. 医生.', '高启强在黑化的道路上越走越远，其实安欣曾多次提醒他要注意自己的行为，但是作为高启强似乎并没有回头的意思，在《狂飙》原著中，其实对于高启强的各 ...', '二、高启强生命中的冷与热，极为分裂，凶狠到极致，爱到极致。人生在世，最难的事情不是无法操控外界，而是反向延展——自控。 对待仇人和利益的抢夺者，他 ...', '高启强善于把握局势，观察他人的各种反应，做出准确的推理判断，并且擅长通过别人的心理变化过程快速反应，用巧劲引导他人为己所用，男女通吃，老少咸宜， ...']\u001b[0m\n",
      "Thought:\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.\n",
      "Calculator: Useful for when you need to answer questions about math.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search, Calculator]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 高启强的扮演者十年之后多少岁?\n",
      "Thought: I should use a search engine to find information about the actor 高启强.\n",
      "Action: Search\n",
      "Action Input: 高启强\n",
      "Observation: ['高启强（1970年～2021年）是电视剧《狂飙》中的男主角。前期为普通市民，中后期为最大反派。该角色由内地男演员张颂文饰演。高启强原本仅是一个底层的卖鱼小贩， ...', '徐纪周向中国新闻周刊表示，高启强原型并非刘汉，原型人物有多个。他说，剧中原型都是扫黑除恶专项斗争开展以来的案例。', '其实，“高启强”有多个现实人物原型，他们有的靠黄赌毒积累原始资产，有的拉拢腐蚀官员，有的肆意欺压百姓……回顾黑恶势力犯罪频发的那个时期，要坚决打击的 ...', '高启强，电视剧《狂飙》中的角色，强盛集团董事长，京海市政协委员，工商联委员，慈善家，由张颂文饰演。高启强致力于社会公益事业，衣着朴素，文质彬彬，是安欣的 ...', '中文名. 高启兰 · 外文名. Gao Qilan · 别名. 小兰（高启强、安欣称呼高启兰） 、兰姐（高启强的手下称呼高启兰） · 饰演. 隆妮 · 性别. 女 · 登场作品. 狂飙 · 职业. 医生.', '高启强在黑化的道路上越走越远，其实安欣曾多次提醒他要注意自己的行为，但是作为高启强似乎并没有回头的意思，在《狂飙》原著中，其实对于高启强的各 ...', '二、高启强生命中的冷与热，极为分裂，凶狠到极致，爱到极致。人生在世，最难的事情不是无法操控外界，而是反向延展——自控。 对待仇人和利益的抢夺者，他 ...', '高启强善于把握局势，观察他人的各种反应，做出准确的推理判断，并且擅长通过别人的心理变化过程快速反应，用巧劲引导他人为己所用，男女通吃，老少咸宜， ...']\n",
      "Thought:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I should look for information about the actor's age in the search results.\n",
      "Action: Search\n",
      "Action Input: 高启强 年龄\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m高启强，1970年生。 警队这边，已知前两任已故队长年龄（碑上有镜头）： 曹闯，1954年3月生。\u001b[0m\n",
      "Thought:\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.\n",
      "Calculator: Useful for when you need to answer questions about math.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search, Calculator]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 高启强的扮演者十年之后多少岁?\n",
      "Thought: I should use a search engine to find information about the actor 高启强.\n",
      "Action: Search\n",
      "Action Input: 高启强\n",
      "Observation: ['高启强（1970年～2021年）是电视剧《狂飙》中的男主角。前期为普通市民，中后期为最大反派。该角色由内地男演员张颂文饰演。高启强原本仅是一个底层的卖鱼小贩， ...', '徐纪周向中国新闻周刊表示，高启强原型并非刘汉，原型人物有多个。他说，剧中原型都是扫黑除恶专项斗争开展以来的案例。', '其实，“高启强”有多个现实人物原型，他们有的靠黄赌毒积累原始资产，有的拉拢腐蚀官员，有的肆意欺压百姓……回顾黑恶势力犯罪频发的那个时期，要坚决打击的 ...', '高启强，电视剧《狂飙》中的角色，强盛集团董事长，京海市政协委员，工商联委员，慈善家，由张颂文饰演。高启强致力于社会公益事业，衣着朴素，文质彬彬，是安欣的 ...', '中文名. 高启兰 · 外文名. Gao Qilan · 别名. 小兰（高启强、安欣称呼高启兰） 、兰姐（高启强的手下称呼高启兰） · 饰演. 隆妮 · 性别. 女 · 登场作品. 狂飙 · 职业. 医生.', '高启强在黑化的道路上越走越远，其实安欣曾多次提醒他要注意自己的行为，但是作为高启强似乎并没有回头的意思，在《狂飙》原著中，其实对于高启强的各 ...', '二、高启强生命中的冷与热，极为分裂，凶狠到极致，爱到极致。人生在世，最难的事情不是无法操控外界，而是反向延展——自控。 对待仇人和利益的抢夺者，他 ...', '高启强善于把握局势，观察他人的各种反应，做出准确的推理判断，并且擅长通过别人的心理变化过程快速反应，用巧劲引导他人为己所用，男女通吃，老少咸宜， ...']\n",
      "Thought: I should look for information about the actor's age in the search results.\n",
      "Action: Search\n",
      "Action Input: 高启强 年龄\n",
      "Observation: 高启强，1970年生。 警队这边，已知前两任已故队长年龄（碑上有镜头）： 曹闯，1954年3月生。\n",
      "Thought:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I should use a calculator to calculate the actor's current age.\n",
      "Action: Calculator\n",
      "Action Input: 2021 - 1970\u001b[0m\n",
      "\n",
      "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
      "2021 - 1970\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mTranslate a math problem into a expression that can be executed using Python's numexpr library. Use the output of running this code to answer the question.\n",
      "\n",
      "Question: ${Question with math problem.}\n",
      "```text\n",
      "${single line mathematical expression that solves the problem}\n",
      "```\n",
      "...numexpr.evaluate(text)...\n",
      "```output\n",
      "${Output of running the code}\n",
      "```\n",
      "Answer: ${Answer}\n",
      "\n",
      "Begin.\n",
      "\n",
      "Question: What is 37593 * 67?\n",
      "```text\n",
      "37593 * 67\n",
      "```\n",
      "...numexpr.evaluate(\"37593 * 67\")...\n",
      "```output\n",
      "2518731\n",
      "```\n",
      "Answer: 2518731\n",
      "\n",
      "Question: 37593^(1/5)\n",
      "```text\n",
      "37593**(1/5)\n",
      "```\n",
      "...numexpr.evaluate(\"37593**(1/5)\")...\n",
      "```output\n",
      "8.222831614237718\n",
      "```\n",
      "Answer: 8.222831614237718\n",
      "\n",
      "Question: 2021 - 1970\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m```text\n",
      "2021 - 1970\n",
      "```\n",
      "...numexpr.evaluate(\"2021 - 1970\")...\n",
      "\u001b[0m\n",
      "Answer: \u001b[33;1m\u001b[1;3m51\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "Observation: \u001b[33;1m\u001b[1;3mAnswer: 51\u001b[0m\n",
      "Thought:\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n",
      "\n",
      "Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.\n",
      "Calculator: Useful for when you need to answer questions about math.\n",
      "\n",
      "Use the following format:\n",
      "\n",
      "Question: the input question you must answer\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Search, Calculator]\n",
      "Action Input: the input to the action\n",
      "Observation: the result of the action\n",
      "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
      "Thought: I now know the final answer\n",
      "Final Answer: the final answer to the original input question\n",
      "\n",
      "Begin!\n",
      "\n",
      "Question: 高启强的扮演者十年之后多少岁?\n",
      "Thought: I should use a search engine to find information about the actor 高启强.\n",
      "Action: Search\n",
      "Action Input: 高启强\n",
      "Observation: ['高启强（1970年～2021年）是电视剧《狂飙》中的男主角。前期为普通市民，中后期为最大反派。该角色由内地男演员张颂文饰演。高启强原本仅是一个底层的卖鱼小贩， ...', '徐纪周向中国新闻周刊表示，高启强原型并非刘汉，原型人物有多个。他说，剧中原型都是扫黑除恶专项斗争开展以来的案例。', '其实，“高启强”有多个现实人物原型，他们有的靠黄赌毒积累原始资产，有的拉拢腐蚀官员，有的肆意欺压百姓……回顾黑恶势力犯罪频发的那个时期，要坚决打击的 ...', '高启强，电视剧《狂飙》中的角色，强盛集团董事长，京海市政协委员，工商联委员，慈善家，由张颂文饰演。高启强致力于社会公益事业，衣着朴素，文质彬彬，是安欣的 ...', '中文名. 高启兰 · 外文名. Gao Qilan · 别名. 小兰（高启强、安欣称呼高启兰） 、兰姐（高启强的手下称呼高启兰） · 饰演. 隆妮 · 性别. 女 · 登场作品. 狂飙 · 职业. 医生.', '高启强在黑化的道路上越走越远，其实安欣曾多次提醒他要注意自己的行为，但是作为高启强似乎并没有回头的意思，在《狂飙》原著中，其实对于高启强的各 ...', '二、高启强生命中的冷与热，极为分裂，凶狠到极致，爱到极致。人生在世，最难的事情不是无法操控外界，而是反向延展——自控。 对待仇人和利益的抢夺者，他 ...', '高启强善于把握局势，观察他人的各种反应，做出准确的推理判断，并且擅长通过别人的心理变化过程快速反应，用巧劲引导他人为己所用，男女通吃，老少咸宜， ...']\n",
      "Thought: I should look for information about the actor's age in the search results.\n",
      "Action: Search\n",
      "Action Input: 高启强 年龄\n",
      "Observation: 高启强，1970年生。 警队这边，已知前两任已故队长年龄（碑上有镜头）： 曹闯，1954年3月生。\n",
      "Thought: I should use a calculator to calculate the actor's current age.\n",
      "Action: Calculator\n",
      "Action Input: 2021 - 1970\n",
      "Observation: Answer: 51\n",
      "Thought:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
      "Final Answer: 高启强的扮演者十年之后51岁。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'高启强的扮演者十年之后51岁。'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "serpapi_api_key = \"259c925ce2f06b0828cdd164dd1df9af4152490fc420c22782bf4da2c96756f3\"\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"], llm=llm, callbacks=[handler], serpapi_api_key=serpapi_api_key, return_direct=True)\n",
    "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_direct=True)\n",
    "agent.run(\"高启强的扮演者十年之后多少岁?\", callbacks=[handler])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'Translate a math problem into a expression that can be executed using Python\\'s numexpr library. Use the output of running this code to answer the question. Question: Question with math problem ```text single line mathematical expression that solves the problem``` ...numexpr.evaluate(text)... ```output Output of running the code``` Answer: Answer Begin Question: What is 37593 * 67? ```text 37593 * 67``` ...numexpr.evaluate(\"37593 * 67\")... ```output 2518731``` Answer: 2518731 Question: 37593^(1/5) ```text 37593**(1/5)``` ...numexpr.evaluate(\"37593**(1/5)\")... ```output 8.222831614237718``` Answer: 8.222831614237718 Question: 4^2'\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/mac/Library/Python/3.9/lib/python/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `BaseLLM.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 0.3.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' ```text 4**2``` ...numexpr.evaluate(\"4**2\")... ```output 16``` Answer: 16'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = repr(\"Translate a math problem into a expression that can be executed using Python\\'s numexpr library. Use the output of running this code to answer the question. \\\n",
    "Question: Question with math problem \\\n",
    "```text single line mathematical expression that solves the problem``` \\\n",
    "...numexpr.evaluate(text)... \\\n",
    "```output Output of running the code``` \\\n",
    "Answer: Answer \\\n",
    "\\\n",
    "Begin \\\n",
    "Question: What is 37593 * 67? \\\n",
    "```text 37593 * 67``` \\\n",
    "...numexpr.evaluate(\\\"37593 * 67\\\")... \\\n",
    "```output 2518731``` \\\n",
    "Answer: 2518731 \\\n",
    "Question: 37593^(1/5) \\\n",
    "```text 37593**(1/5)``` \\\n",
    "...numexpr.evaluate(\\\"37593**(1/5)\\\")... \\\n",
    "```output 8.222831614237718``` \\\n",
    "Answer: 8.222831614237718 \\\n",
    "Question: 4^2\")\n",
    "print(prompt)\n",
    "llm(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I should use a calculator to calculate the actor\\'s age based on the information I found.\"\\\\n  Action: Calculator\\\\n  Action Input: 2021-1970\\\\n  Observation: 51\\\\n  Thought: \"I now know that the actor would be 51 years old in 2021.\"\\\\n  Final Answer: 51'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = repr(\"Answer the following questions as best you can. You have access to the following tools: \\\n",
    "              Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query. \\\n",
    "              Calculator: Useful for when you need to answer questions about math.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [Search, Calculator]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\n \\\n",
    "\\\n",
    "Begin!\\n\\n \\\n",
    " \\\n",
    "Question: 高启强的扮演者十年之后多少岁?\\n \\\n",
    "Thought: I should use a search engine to find information about the actor 高启强.\\n \\\n",
    "Action: Search\\n \\\n",
    "Action Input: 高启强\\n \\\n",
    "Observation: ['高启强（1970年～2021年）是电视剧《狂飙》中的男主角。前期为普通市民，中后期为最大反派。该角色由内地男演员张颂文饰演。高启强原本仅是一个底层的卖鱼小贩， ...', '徐纪周向中国新闻周刊表示，高启强原型并非刘汉，原型人物有多个。他说，剧中原型都是扫黑除恶专项斗争开展以来的案例。', '其实，“高启强”有多个现实人物原型，他们有的靠黄赌毒积累原始资产，有的拉拢腐蚀官员，有的肆意欺压百姓……回顾黑恶势力犯罪频发的那个时期，要坚决打击的 ...', '高启强，电视剧《狂飙》中的角色，强盛集团董事长，京海市政协委员，工商联委员，慈善家，由张颂文饰演。高启强致力于社会公益事业，衣着朴素，文质彬彬，是安欣的 ...', '中文名. 高启兰 · 外文名. Gao Qilan · 别名. 小兰（高启强、安欣称呼高启兰） 、兰姐（高启强的手下称呼高启兰） · 饰演. 隆妮 · 性别. 女 · 登场作品. 狂飙 · 职业. 医生.', '高启强在黑化的道路上越走越远，其实安欣曾多次提醒他要注意自己的行为，但是作为高启强似乎并没有回头的意思，在《狂飙》原著中，其实对于高启强的各 ...', '二、高启强生命中的冷与热，极为分裂，凶狠到极致，爱到极致。人生在世，最难的事情不是无法操控外界，而是反向延展——自控。 对待仇人和利益的抢夺者，他 ...', '高启强善于把握局势，观察他人的各种反应，做出准确的推理判断，并且擅长通过别人的心理变化过程快速反应，用巧劲引导他人为己所用，男女通吃，老少咸宜， ...']\\n  \\\n",
    "Thought: I should look for information about the actor's age in the search results.\\n  \\\n",
    "Action: Search\\n  \\\n",
    "Action Input: 高启强 年龄\\n  \\\n",
    "Observation: 高启强，1970年生。 警队这边，已知前两任已故队长年龄（碑上有镜头）： 曹闯，1954年3月生。\\n  \\\n",
    "Thought:\")\n",
    "llm(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显式创建\n",
    "# prompt = hub.pull(\"hwchase17/structured-chat-agent\")\n",
    "\n",
    "# agent = create_json_chat_agent(llm, tools, prompt)\n",
    "\n",
    "# query = \"影视剧演员邓超的妻子是谁？\"\n",
    "\n",
    "# agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)\n",
    "# result = agent_executor.invoke({\"input\": query})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.tools import BaseModel, BaseTool\n",
    "from langchain_core.pydantic_v1 import Field\n",
    "from typing import Any, Optional, Sequence, Type\n",
    "\n",
    "class ToolInput(BaseModel):\n",
    "    args1: str = Field(description=\"the desc for args1\")\n",
    "    args2: str = Field(description=\"the desc for args2\")\n",
    "    args3: str = Field(description=\"the desc for args3\", default=\"\")\n",
    "\n",
    "class ToolFunction(BaseTool):\n",
    "    name = \"tool_name\"\n",
    "    description = \"the desc for tool_name\"\n",
    "    args_schema: Type[BaseModel] = ToolInput\n",
    "    return_direct = True\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "\n",
    "    def _run(self, args1: str, args2: str, args3: Optional[str] = \"\") -> dict[str, Any]:\n",
    "        return \"tool result\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
